package cn.edu.scau.dropship.controller;

import cn.edu.scau.dropship.entity.*;
import cn.edu.scau.dropship.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private UserService userService;
    @Autowired
    private DictionaryService dictionaryService;
    @Autowired
    private ParameterService parameterService;
    @Autowired
    private FinanceService financeService;
    @Autowired
    private TraceService traceService;
    @Autowired
    private WalletService walletService;

    //获取管理员信息
    @PostMapping(value = "/getInfo")
    @ResponseBody
    public User findById(HttpServletRequest request){
        String str_id=request.getParameter("id");
        Integer int_id=Integer.valueOf(str_id);
        User user=userService.selectByPrimaryKey(int_id);
        return user;
    }

    // 查询finance、trace、wallet三表，组装数据完整交易记录
    @RequestMapping(value = "/getFinList",method = RequestMethod.POST)
    @ResponseBody
    public List<Object> getFinList(HttpServletResponse response){
        List<Object> resultlist=new ArrayList<>();
        List<Finance_check> flist=financeService.getlist();
        List<TraceKey> tlist=new ArrayList<>();
        for(int i=0;i<flist.size();i++){
            TraceKey tk=traceService.selectByfid(flist.get(i).getId());
            Wallets wallets=walletService.selectByPrimaryKey(tk.getWalletsId());
            resultlist.add(flist.get(i));
            resultlist.add(wallets);
        }
        return resultlist;
    }

    //修改管理员信息
    @PostMapping(value = "/updateInfo")
    @ResponseBody
    public String updateInfo(HttpServletRequest request){
        Integer uid=Integer.valueOf(request.getParameter("id"));
        User user=userService.selectByPrimaryKey(uid);

        //user.setName(request.getParameter("name"));
        String email=request.getParameter("email");
        String phone=request.getParameter("phone");
        if(email.length()>30){
            return "false";
        }
        if(phone.length()>11){
            return "false";
        }
        user.setEmail(email);
        user.setPhone(phone);

        int result=userService.updateByPrimaryKey(user);
        if(result>0){
            return "true";
        }
        return "false";
    }

    //修改管理员密码
    @RequestMapping(value = "/updatePwd",method = RequestMethod.POST)
    @ResponseBody
    public String updatePwd(HttpServletRequest request){
        Integer uid=Integer.valueOf(request.getParameter("id"));
        User user=userService.selectByPrimaryKey(uid);

        String opw=request.getParameter("opw");
        String npw1=request.getParameter("npw1");
        String npw2=request.getParameter("npw2");

        //对输入判断
        if(!opw.equals(user.getPassword())){
            return "输入的旧密码不正确，请重新输入！";
        }
        else if(!npw1.equals(npw2)){
            return "输入的两次密码不一致,请重新输入!";
        }

        user.setPassword(npw1);
        int result=userService.updateByPrimaryKey(user);
        if(result>0){
            return "修改密码成功!";
        }
        return "修改密码成功!";
    }
}
